package sep.test;

import java.awt.Component;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import sep.test.IdaLanguage.Language;

public class IdaSettingsChanger extends JFrame {

	private static final long serialVersionUID = 1L;

	private final IdaSettings settings = IdaSettings.getInstance();
	private final Component parent;

	private JCheckBox chckbxShowLane;
	private JRadioButton rdbtnShowOriginal;
	private JRadioButton rdbtnShowCanny;
	private JComboBox cboLanguage;
	private JTextField txtRotationX;
	private JTextField txtRotationY;
	private JTextField txtRotationZ;
	private JTextField txtTranslationX;
	private JTextField txtTranslationY;
	private JTextField txtTranslationZ;
	private JTextField txtCamCenterX;
	private JTextField txtCamCenterY;
	private JTextField txtCamParamA;
	private JTextField txtCamParamB;
	private JTextField txtCamParamC;

	public IdaSettingsChanger(Component parent) {
		this.parent = parent;

		initDialog();
	}

	private void initDialog() {
		setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		setBounds(0, 0, 640, 480);
		setMinimumSize(getSize());
		setResizable(false);

		getContentPane().setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));

		rdbtnShowOriginal = new JRadioButton("Originalbild anzeigen");
		rdbtnShowOriginal.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showImage();
			}
		});
		getContentPane().add(rdbtnShowOriginal);

		rdbtnShowCanny = new JRadioButton("Kantenbild anzeigen");
		rdbtnShowCanny.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				showImage();
			}
		});
		getContentPane().add(rdbtnShowCanny);

		ButtonGroup groupImage = new ButtonGroup();
		groupImage.add(rdbtnShowOriginal);
		groupImage.add(rdbtnShowCanny);

		chckbxShowLane = new JCheckBox("Fahrspur einzeichnen");
		chckbxShowLane.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				settings.setDrawLane(chckbxShowLane.isSelected());
			}
		});
		getContentPane().add(chckbxShowLane);

		cboLanguage = new JComboBox(Language.values());
		cboLanguage.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				settings.setLanguage((Language) cboLanguage.getSelectedItem());
			}
		});
		getContentPane().add(cboLanguage);

		JLabel lblRotation = new JLabel("Rotation");
		getContentPane().add(lblRotation);

		txtRotationX = new JTextField();
		getContentPane().add(txtRotationX);
		txtRotationX.setColumns(10);

		txtRotationY = new JTextField();
		txtRotationY.setColumns(10);
		getContentPane().add(txtRotationY);

		txtRotationZ = new JTextField();
		txtRotationZ.setColumns(10);
		getContentPane().add(txtRotationZ);

		JButton btnSetCamParams = new JButton("Kameraparameter setzen");
		btnSetCamParams.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				final IdaCameraModel cam = settings.getCameraModel();
				cam.setRotationAngles(Float.parseFloat(txtRotationX.getText()),
						Double.parseDouble(txtRotationY.getText()),
						Double.parseDouble(txtRotationZ.getText()));
				cam.setTranslation(Float.parseFloat(txtTranslationX.getText()),
						Double.parseDouble(txtTranslationY.getText()),
						Double.parseDouble(txtTranslationZ.getText()));
				cam.setCameraParameters(
						Double.parseDouble(txtCamParamA.getText()),
						Double.parseDouble(txtCamParamB.getText()),
						Double.parseDouble(txtCamParamC.getText()),
						Double.parseDouble(txtCamCenterX.getText()),
						Double.parseDouble(txtCamCenterY.getText()));
			}
		});

		JLabel lblTranslation = new JLabel("Translation");
		getContentPane().add(lblTranslation);

		txtTranslationX = new JTextField();
		txtTranslationX.setColumns(10);
		getContentPane().add(txtTranslationX);

		txtTranslationY = new JTextField();
		txtTranslationY.setColumns(10);
		getContentPane().add(txtTranslationY);

		txtTranslationZ = new JTextField();
		txtTranslationZ.setColumns(10);
		getContentPane().add(txtTranslationZ);

		JLabel lblKameraZentrum = new JLabel("Kamera Zentrum");
		getContentPane().add(lblKameraZentrum);

		txtCamCenterX = new JTextField();
		txtCamCenterX.setColumns(10);
		getContentPane().add(txtCamCenterX);

		txtCamCenterY = new JTextField();
		txtCamCenterY.setColumns(10);
		getContentPane().add(txtCamCenterY);

		JLabel lblKameraParameter = new JLabel("Kamera Parameter");
		getContentPane().add(lblKameraParameter);

		txtCamParamA = new JTextField();
		txtCamParamA.setColumns(10);
		getContentPane().add(txtCamParamA);

		txtCamParamB = new JTextField();
		txtCamParamB.setColumns(10);
		getContentPane().add(txtCamParamB);

		txtCamParamC = new JTextField();
		txtCamParamC.setColumns(10);
		getContentPane().add(txtCamParamC);
		getContentPane().add(btnSetCamParams);
	}

	private void showImage() {
		settings.setShowCannyImage(rdbtnShowCanny.isSelected());
	}

	private void loadData() {
		rdbtnShowCanny.setSelected(settings.isShowCannyImage());
		chckbxShowLane.setSelected(settings.isDrawLane());
		cboLanguage.setSelectedItem(settings.getLanguage());

		final IdaCameraModel cam = settings.getCameraModel();
		double[] vals = cam.getRotationAngles();
		txtRotationX.setText(String.valueOf(vals[0]));
		txtRotationY.setText(String.valueOf(vals[1]));
		txtRotationZ.setText(String.valueOf(vals[2]));

		vals = cam.getTranslation();
		txtTranslationX.setText(String.valueOf(vals[0]));
		txtTranslationY.setText(String.valueOf(vals[1]));
		txtTranslationZ.setText(String.valueOf(vals[2]));

		vals = cam.getCameraParameters();
		txtCamParamA.setText(String.valueOf(vals[0]));
		txtCamParamB.setText(String.valueOf(vals[1]));
		txtCamParamC.setText(String.valueOf(vals[2]));
		txtCamCenterX.setText(String.valueOf(vals[3]));
		txtCamCenterY.setText(String.valueOf(vals[4]));
	}

	@Override
	public void setVisible(boolean b) {
		if (b) {
			setLocationRelativeTo(parent);
			loadData();
		}

		super.setVisible(b);
	}
}
